﻿// MIT License
//
// Copyright (c) 2021-present zuohuaijun, Daming Co.,Ltd and Contributors
//
// 电话/微信：18020030720 QQ群1：87333204 QQ群2：252381476

using Admin.NET.Application.Const;
using Admin.NET.Core.Service;
using Admin.Net.InformationPlatform.Entity;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Nest;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Admin.NET.Core;
using Admin.Net.InformationPlatform.Service.Maintenance.Dto;
using SjEntityClass;
using Furion.FriendlyException;

namespace Admin.Net.InformationPlatform.Service.Maintenance
{
    /// <summary>
    /// 运维平台
    /// </summary>
    [ApiDescriptionSettings(groups: "Maintenance", Name = "FQC管理", Tag = "FQC管理", Order = 200)]

    public class FQCService : IDynamicApiController, ITransient
    {
        private readonly SysUserRoleService _sysUserRoleService;
        /// <summary>
        /// 单例数据链接
        /// </summary>
        ISqlSugarClient db;
        /// <summary>
        /// T100数据库
        /// </summary>
        private SqlSugarProvider _T100Db;
        private SqlSugarProvider _SjDb;
        private SqlSugarProvider _plm;
        private SqlSugarProvider _Dilon;


        /// <summary>
        /// 用户信息
        /// </summary>
        TokenUser _User;
        /// <summary>
        /// 成品仓库管理模块
        /// </summary>
        /// <param name="db"></param>
        public FQCService(ISqlSugarClient db, SysUserRoleService sysUserRoleService)
        {
            this.db = db;
            this._T100Db = this.db.AsTenant().GetConnection(T100TestArea.ConfigId);
            this._SjDb = this.db.AsTenant().GetConnection(SJDB.ConfigId);
            this._plm = this.db.AsTenant().GetConnection(PLM.ConfigId);
            this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);
            // 获取 `Jwt` 存储的信息 企业编号与据点信息
            _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db, _Dilon);
            _sysUserRoleService = sysUserRoleService;

        }
        /// <summary>
        ///FQC管理
        ///1.根据品号查询检验单号
        /// </summary>
        /// <returns></returns>
        [HttpPost("/FQC/GeFQCList")]
        public async Task<SqlSugarPagedList<QcbaT>> GetCustomerPagedList(FQCInput input)
        {
            var output = await _T100Db.Queryable<QcbaT>()
    .WhereIF(!string.IsNullOrEmpty(input.ProductNo), (l) => (l.Qcba010.Contains(input.ProductNo)))//根据料号查询
    .WhereIF(!string.IsNullOrEmpty(input.QcbadocNo), (l) => (l.Qcbadocno.Contains(input.QcbadocNo)))//根据单号查询
    .Where((l) => (l.Qcbaent == 8))
    .Where((l) => (l.Qcbasite =="SJGS"))
    .Where((l) => (l.Qcba000 =="2"))
    .Where((l) => (l.Qcbastus =="Y"))
    .OrderBy(l => l.Qcbadocdt, OrderByType.Desc) //单据日期排序
    .Select(l => new QcbaT
    {
       Qcbaent = l.Qcbaent,
       Qcbasite = l.Qcbasite,
       Qcbadocno = l.Qcbadocno,
       Qcbadocdt = l.Qcbadocdt,
       Qcba000= l.Qcba000,
       Qcba001 = l.Qcba001,
       Qcba002 = l.Qcba002,
       Qcba003 = l.Qcba003,
       Qcba004 = l.Qcba004,
       Qcba005 = l.Qcba005,
       Qcba006 = l.Qcba006,
       Qcba007 = l.Qcba007,
       Qcba008 = l.Qcba008,
       Qcba009 = l.Qcba009,
       Qcba010 = l.Qcba010,
       Qcba011 = l.Qcba011,
       Qcba012 = l.Qcba012,
       Qcba013 = l.Qcba013,
       Qcba014 = l.Qcba014,
       Qcba015 = l.Qcba015,
       Qcba016 = l.Qcba016,
       Qcba017 = l.Qcba017,
       Qcba018 = l.Qcba018,
       Qcba019 = l.Qcba019,
       Qcba020 = l.Qcba020,
       Qcba021 = l.Qcba021,
       Qcba022 = l.Qcba022,
       Qcba023 = l.Qcba023,
       Qcba024 = l.Qcba024,
       Qcba025 = l.Qcba025,
       Qcba026 = l.Qcba026,
       Qcba027 = l.Qcba027,
       Qcba028 = l.Qcba028,
       Qcba029 = l.Qcba029,
       Qcba030  = l.Qcba030,
       Qcba031 = l.Qcba031,
       Qcba032 = l.Qcba032,
       Qcba033 = l.Qcba033,
       Qcba034 = l.Qcba034,
       Qcba035 = l.Qcba035,
       Qcba900 = l.Qcba900,
       Qcba901 = l.Qcba901,
       Qcbacnfdt = l.Qcbacnfdt,
       Qcbacnfid = l.Qcbacnfid,
       Qcbacrtdp = l.Qcbacrtdp,
       Qcbacrtdt = l.Qcbacrtdt,
       Qcbacrtid = l.Qcbacrtid,
       Qcbamoddt = l.Qcbamoddt,
       Qcbamodid = l.Qcbamodid,
       Qcbaowndp = l.Qcbaowndp,
       Qcbaownid = l.Qcbaownid,
       Qcbapstdt = l.Qcbapstdt,
       Qcbapstid = l.Qcbapstid,
       Qcbastus = l.Qcbastus,
       Qcbaud001 = l.Qcbaud001,
       Qcbaud002 = l.Qcbaud002,
       Qcbaud003 = l.Qcbaud003,
       Qcbaud004 = l.Qcbaud004,
       Qcbaud005 = l.Qcbaud005,
       Qcbaud006 = l.Qcbaud006,
       Qcbaud007 = l.Qcbaud007,
       Qcbaud008 = l.Qcbaud008,
       Qcbaud009 = l.Qcbaud009,
       Qcbaud010 = l.Qcbaud010,
       Qcbaud011 = l.Qcbaud011,
       Qcbaud012 = l.Qcbaud012,
       Qcbaud013 = l.Qcbaud013,
       Qcbaud014 = l.Qcbaud014,
       Qcbaud015 = l.Qcbaud015,
       Qcbaud016 = l.Qcbaud016,
       Qcbaud017 = l.Qcbaud017,
       Qcbaud018 = l.Qcbaud018,
       Qcbaud019 = l.Qcbaud019,
       Qcbaud020 = l.Qcbaud020,
       Qcbaud021 = l.Qcbaud021,
       Qcbaud022 = l.Qcbaud022,
       Qcbaud023 = l.Qcbaud023,
       Qcbaud024 = l.Qcbaud024,
       Qcbaud025 = l.Qcbaud025,
       Qcbaud026 = l.Qcbaud026,
       Qcbaud027 = l.Qcbaud027,
       Qcbaud028 = l.Qcbaud028,
       Qcbaud029 = l.Qcbaud029,
       Qcbaud030 = l.Qcbaud030,
    }).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }


        /// <summary>
        /// FQC异常信息处理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/FQC/UpdateFQCData")]
        public async Task<string> UpdateFQCData(FQCUpdate input)
        {
            //判断单据存在
            int cnt = await _T100Db.Queryable<QcbaT>()
                .Where(a => a.Qcbadocno == input.QcbadocNo)
                .Select(a => a.Qcbadocno)
                .CountAsync();
            if (cnt == 0)
            {
                throw Oops.Oh("任务不存在，驳回！");
            }
            else
            {
                //提交
                try
                {
                    _SjDb.Ado.BeginTran();
                    int count = await _T100Db.Updateable<QcbaT>()
                        .SetColumns(a => a.Qcbadocdt == input.Qcbadocdt)
                        .SetColumns(a => a.Qcba001 == input.Qcba001)
                        .SetColumns(a => a.Qcba002 == input.Qcba002)
                        .SetColumns(a => a.Qcba017 == input.Qcba017)
                        .SetColumns(a => a.Qcba024 == input.Qcba024)
                        .SetColumns(a => a.Qcba016 == input.Qcba016)
                        .SetColumns(a => a.Qcba021 == input.Qcba021)
                        .SetColumns(a => a.Qcba023 == input.Qcba023)
                        .SetColumns(a => a.Qcba027 == input.Qcba027)
                        .Where(a => a.Qcbadocno == input.QcbadocNo)
                        .ExecuteCommandAsync();

                    if (count == 0)
                    {
                        throw Oops.Oh("修改失败！");
                    }
                    _SjDb.Ado.CommitTran();
                    return "修改成功！";
                }
                catch (Exception e)
                {
                    _SjDb.Ado.RollbackTran();
                    throw Oops.Oh($"异常：{e.Message}");
                }
            }

        }
    }

}

